#include<bits/stdc++.h>
using namespace std;
//open long long remember
#define ll long long
long long n,m,a[100010],c[100010],temp,ansf=0,T,x[10],k=0,ma,d[50][100010],ans[100010],ss[100010];
long long pw2(long long xxx)
{

    if(xxx==0) return 1;
    if(xxx==1) return 2;
    long long tpm=pw2(xxx/2);
    return tpm*tpm*pw2(xxx%2);
}
int main()
{
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    for(int i=1;i<=m;i++)
    {
        cin>>c[m];
    }
    temp=1;
    while(temp<n)
    {
        k++;
        temp*=2;
    }
    for(int i=1;i<=k;i++)
    {
        ma=pw2(k-i);
        for(int j=1;j<=ma;j++)
        {
            cin>>d[i][j];
        }
    }
    cin>>T;
    while(T--)
    {
        for(int i=0;i<=3;i++)
        {
            cin>>x[i];
        }
        for(int i=1;i<=n;i++)
        {
            a[i]=a[i]^x[i%4];
            ss[i]=a[i];
            ans[i]=i;
        }
        for(int i=1;i<=m;i++)
        {
            for(int i=1;i<=n;i++)
            {
                ss[i]=a[i];
                ans[i]=i;
            }
            temp=1;k=0;
            while(temp<c[i])
            {
                k++;
                temp*=2;
            }
            for(int i=1;i<=k;i++)
            {
                ma=pw2(k-i);
                for(int j=1;j<=ma;j++)
                {
                    if(d[i][j]==0)
                    {
                        if(ss[2*j-1]>=i)
                        {
                            ss[j]=ss[2*j-1];
                            ans[j]=ans[2*j-1];
                        }
                        else
                        {
                            ss[j]=ss[2*j];
                            ans[j]=ans[2*j];
                        }
                    }
                    else
                    {
                        if(ss[2*j]>=i)
                        {
                            ss[j]=ss[2*j];
                            ans[j]=ans[2*j];
                        }
                        else
                        {
                            ss[j]=ss[2*j-1];
                            ans[j]=ans[2*j-1];
                        }
                    }
                }
            }
            ansf=ansf^(i*ans[1]);
        }
        cout<<ansf<<endl;
    }

    return 0;
}
